package com.example.operation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.operation.domin.SysBusinessArea;
import com.example.operation.dto.SysBusinessAreaDto;
import com.example.operation.exception.CommonException;
import com.example.operation.mapper.SysBusinessAreaMapper;
import com.example.operation.service.SysBusinessAreaService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysBusinessAreaServiceImpl extends ServiceImpl<SysBusinessAreaMapper,SysBusinessArea> implements SysBusinessAreaService {

    @Autowired
    private SysBusinessAreaMapper sysBusinessAreaMapper;


    @Override
    public Page<SysBusinessArea> pageSBA(Integer pageNumber, Integer pageSize, Integer name) {
        if(name != null){
        pageNumber = name;
        }
        Page<SysBusinessArea> page = new Page<>(pageNumber, pageSize);
        LambdaQueryWrapper<SysBusinessArea> lambda = new QueryWrapper<SysBusinessArea>()
                .lambda().eq(SysBusinessArea::getState, 1);

        return sysBusinessAreaMapper.selectPage(page, lambda);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertSBA(SysBusinessAreaDto sysBusinessAreaDto) {
        SysBusinessArea sysBusinessArea = new SysBusinessArea();
        LambdaQueryWrapper<SysBusinessArea> eq = new QueryWrapper<SysBusinessArea>()
                .lambda().eq(SysBusinessArea::getName, sysBusinessArea.getName())
                .eq(SysBusinessArea::getState, 0);
        Integer count = sysBusinessAreaMapper.selectCount(eq);
        if (count > 0) {
            throw new RuntimeException("区/县名称不能重复");
        }
        BeanUtils.copyProperties(sysBusinessAreaDto,sysBusinessArea);
        sysBusinessArea.setLat(sysBusinessAreaDto.getLat().toString());
        sysBusinessArea.setLng(sysBusinessAreaDto.getLng().toString());
        sysBusinessAreaMapper.insert(sysBusinessArea);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int delectById(Integer id) {
        return sysBusinessAreaMapper.deleteById(id);
    }

   /* @Override
    public List<String> saveMap() {
        LambdaQueryWrapper<SysBusinessArea> eq = new QueryWrapper<SysBusinessArea>()
                .lambda().eq(SysBusinessArea::getState, 1);
        Integer count = sysBusinessAreaMapper.selectCount(eq);
        //list中各个对象的名字取出来组成一个新的集合
        if (count > 0) {
            List<SysBusinessArea> list = sysBusinessAreaMapper.selectList(eq);
            return list.stream().map(item -> {
                return item.getName();
            }).collect(Collectors.toList());
        } else {
            throw new CommonException("无此地区");
        }

    }
*/
    @Override
    public SysBusinessArea selectById(Integer id) {
        return sysBusinessAreaMapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int update(SysBusinessAreaDto sysBusinessAreaDto) {
        SysBusinessArea sysBusinessArea = new SysBusinessArea();
        LambdaUpdateWrapper<SysBusinessArea> eq = new UpdateWrapper<SysBusinessArea>()
                .lambda().eq(SysBusinessArea::getId,sysBusinessAreaDto.getId())
                .eq(SysBusinessArea::getState,1);
        BeanUtils.copyProperties(sysBusinessAreaDto,sysBusinessArea);
        return sysBusinessAreaMapper.update(sysBusinessArea,eq);
    }
}
